package com.sheng.config;

/**
 * @Author: 王胜
 * @Date 2022/11/20 15:50
 */
import com.sheng.interceptor.JWTInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.ArrayList;
import java.util.List;

@Configuration
@Slf4j
public class WebMVCConfig extends WebMvcConfigurationSupport {


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/jmeterReport/**")
                .addResourceLocations("file:D:/back_code/uploadJemter/");
    }


    private List<String> urls = new ArrayList<>();


    /**
     * 添加拦截器的方式，可直接new一个对象，
     * registry.addInterceptor(new ParamInterceptor())，
     * 但通过手动new出来的拦截器中，无法使用@Autowired 和 @Value 加载对象和配置文件参数。
     *
     * 所以需要在添加拦截器此处，通过@Bean 注解，意味着将这个对象
     * 交给spring管理。那么该拦截器才可以使用@Value等spring管理的注解
     * @return
     */
    @Bean
    public JWTInterceptor jwtInterceptor(){
        return new JWTInterceptor();
    }

    @Autowired
    private IgnoreUrlsConfig ignoreUrlsConfig;

    /**
     * 注册拦截器
     * */
    @Override
    public void addInterceptors(InterceptorRegistry registry){
        System.out.println("WebMVCConfig-----------------------");
        urls = ignoreUrlsConfig.getUrls();
        log.info(ignoreUrlsConfig.getUrls() + "");
        registry.addInterceptor(jwtInterceptor())
                .addPathPatterns("/**") //拦截的地址
                .excludePathPatterns(urls); //不需要拦截的地址，如登录接口
    }



}